package com.example.devicehelloworld.controller;


import com.isyscore.os.dev.api.dmc.model.builder.GetAttributeValueRequestBuilder;
import com.isyscore.os.dev.api.dmc.model.builder.InvokeServiceRequestBuilder;
import com.isyscore.os.dev.api.dmc.model.builder.SetAttributeValueRequestBuilder;
import com.isyscore.os.dev.api.dmc.model.result.GetAttributeValueResult;
import com.isyscore.os.dev.api.dmc.model.result.InvokeServiceResult;
import com.isyscore.os.dev.api.dmc.model.result.SetAttributeValueResult;
import com.isyscore.os.dev.api.dmc.service.DmcService;
import com.isyscore.os.dev.api.permission.model.builder.OsLoginRequestBuilder;
import com.isyscore.os.dev.api.permission.model.result.OsLoginResult;
import com.isyscore.os.dev.api.permission.service.PermissionService;
import com.isyscore.os.dev.util.IsyscoreHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/${api-module}/car")
public class DeviceController {

    private final DmcService dmcService;

    private final PermissionService permissionService;

    public DeviceController(DmcService dmcService, PermissionService permissionService) {
        this.dmcService = dmcService;
        this.permissionService = permissionService;
    }

    /**
     * 登陆
     * @param builder 登录参数封装
     */
    @PostMapping("/login")
    public String login(@RequestBody OsLoginRequestBuilder builder) {
        builder.setHeaders(new IsyscoreHashMap());
        OsLoginResult result = permissionService.osLogin(builder);
        return result.isSuccess() ? result.getResponse().getBody() : result.getResponse().getMessage();
    }

    /**
     * 获取属性
     *
     * @param request 当前请求信息
     * @param builder 获取属性参数封装
     * @see GetAttributeValueRequestBuilder 提供了具体入参信息
     */
    @GetMapping("/getAttributeValue")
    public String getAttributeValue(HttpServletRequest request, @RequestBody GetAttributeValueRequestBuilder builder) {
        builder.setRequestHeader(request);
        GetAttributeValueResult result = dmcService.getAttributeValue(builder);
        return result.isSuccess() ? result.getResponse().getBody() : result.getResponse().getMessage();
    }

    /**
     * 设置属性
     *
     * @param request 当前请求信息
     * @param builder 设置属性的入参封装
     * @see SetAttributeValueRequestBuilder 提供了具体入参信息
     */
    @PostMapping("/setAttributeValue")
    public String setAttributeValue(HttpServletRequest request, @RequestBody SetAttributeValueRequestBuilder builder) {
        builder.setRequestHeader(request);
        SetAttributeValueResult result = dmcService.setAttributeValue(builder);
        return result.isSuccess() ? result.getResponse().getBody() : result.getResponse().getMessage();
    }

    /**
     * 调用服务
     *
     * @param request 当前请求信息
     * @param builder 调用服务的入参封装
     * @see InvokeServiceRequestBuilder 提供了具体入参信息
     */
    @PostMapping("/invokeService")
    public String invokeService(HttpServletRequest request, @RequestBody InvokeServiceRequestBuilder builder) {
        builder.setRequestHeader(request);
        InvokeServiceResult result = dmcService.invokeService(builder);
        return result.isSuccess() ? result.getResponse().getBody() : result.getResponse().getMessage();
    }
}
